package com.example.androidlearning2200.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidlearning2200.R;
import com.example.androidlearning2200.bean.ContactBean;
import com.example.androidlearning2200.bean.EntryBean;

import java.util.List;
import java.util.Random;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.RVHolder> {

    private List<ContactBean> datas;

    public ContactAdapter(List<ContactBean> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_contact, parent, false);
        return new RVHolder(view);
    }

    /**
     * 给每一个列表项填充内容
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull RVHolder holder, int position) {
        ContactBean contactBean = datas.get(position);
        Random random = new Random();
        holder.tvFlag.setBackgroundColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        holder.tvName.setText(contactBean.getName());
        holder.tvPhone.setText(contactBean.getPhone());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    /**
     * 列表项包装器
     */
    public class RVHolder extends RecyclerView.ViewHolder {

        private TextView tvFlag;
        private TextView tvName;
        private TextView tvPhone;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvFlag = itemView.findViewById(R.id.tvFlag);
            tvName = itemView.findViewById(R.id.tvName);
            tvPhone = itemView.findViewById(R.id.tvPhone);
        }
    }

}
